package com.oocl.servlet;

import static com.oocl.util.IPPropertiesUtil.getProperty;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/local/*")
public class LocalResourcesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private final String MERCHANT_SERVER_URL = String.format("http://%s:%s/O2OMerchant/local", getProperty("merchant.ipaddr"), getProperty("merchant.port"));
//	private final String MERCHANT_SERVER_URL = "http://10.222.29.186:8080/O2OMerchant/local";
//	private final String MERCHANT_SERVER_URL = "http://10.222.29.181:8081/O2OMerchant/local";

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String requestUri = request.getRequestURI();
		String regex = "^\\" + request.getContextPath() + "\\/local(\\/.*)";
		Matcher matcher = Pattern.compile(regex).matcher(requestUri);
		matcher.matches();
		
		String remoteUrl = MERCHANT_SERVER_URL + matcher.group(1);
		response.sendRedirect(remoteUrl);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
